﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class univercityunit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (null != Session["person"])
            {
                PersonClass pc = new PersonClass();
                if (pc.ishm(Session["person"].ToString()).Equals("1"))
                {
                    //SqlDataSource1.InsertCommand = "INSERT INTO [tb_hm] ([pid], [hmname], [hmage], [hmsex], [hmtel]) VALUES ('" + Session["person"].ToString() + "', @hmname, @hmage, @hmsex, @hmtel)";
                    
                }
                else
                {
                    Label1.Text = "<script>alert('您的权限不足！');window.location.href='Default.aspx';</script>";
                    return;
                }
            }
            else
            {
                Label1.Text = "<script>alert('请登录！');window.location.href='Default.aspx';</script>";
            }
            this.bind();
            this.bindtoddl();
        }
    }
    public void bind()
    {
        get g = new get();
        string strname = g.getnamesession((string)Session["person"]);
        DataSet ds = new get().getupersoninfo(strname);
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
    public void bindtoddl()
    {
        DataSet ds = new get().getschoolclass();
        ddl.DataSource = ds;
       // ddl.DataTextField = "uuname";
        ddl.DataValueField = "uuname";
        ddl.DataBind();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        
        try
        {
            string name = tbxname.Text.Trim();
            string add = tbxadd.Text.Trim();
            string tel = tbxtel.Text.Trim();
            string sex = "";
            if (RadioButton1.Checked)
            { sex = "男"; }
            if(RadioButton2.Checked)
            {
                sex="女";
            }
            int age = Convert.ToInt32(tbxage.Text.Trim());
            int indexuuid=new get().getschoolid(ddl.SelectedValue.ToString());
            string handle = ddlhandle.SelectedValue.ToString();
            get g = new get();

            string strname = g.getnamesession((string)Session["person"]);
            SqlConnection con = ConDB.GetOpen();
            string sql = "insert into upersoninfo values('" + name + "','" + add + "','" + tel + "','" + sex + "'," + age + "," + indexuuid + ",'" + handle + "','" + DateTime.Now.ToShortDateString() + "','" + strname + "')";
            
            SqlCommand cmd = new SqlCommand(sql, con);
            int n= cmd.ExecuteNonQuery();
            if (n > 0)
            {
             //   Label1.Text = "<script>alert('插入成功!');</script>";
                this.bind();
                this.clean();
            }
            else
            {
                Label1.Text = "<script>alert('插入失败!');</script>";
            }
        }
        catch
        {
            
        }
        finally
        {
            ConDB.Close();
        }
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        this.clean();
    }
    public void clean()
    {

        tbxname.Text = "";
        tbxadd.Text = "";
        tbxtel.Text = "";
        tbxage.Text = "";
        ddl.SelectedIndex = 0;
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        bind();
    }
}
